using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Services
{
    public class Meter_InspectionHistoryServices : BaseServices<InspectionHistory>, IMeter_InspectionHistoryServices
    {
        private readonly IMeter_InspectionHistoryRepository _repository;
        private readonly SqlSugarClient _client;

        public Meter_InspectionHistoryServices(SqlSugarClient client, IMeter_InspectionHistoryRepository repository) : base(repository)
        {
            _repository = repository;
            _client = client;
        }
        /// <summary>
        /// 巡检历史记录
        /// </summary>
        /// <param name="bootstrap"></param>
        /// <returns></returns>
        public string PageList(Bootstrap.BootstrapParams bootstrap)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<InspectionHistory,ClientInfo,DeviceInfo, AllLevelUserClientInfoView>
                ((s, u,w,x) => new object[] {
                   JoinType.Left,s.ClientId==u.ClientId,
                   JoinType.Left,s.DeviceId==w.DeviceId,
                   JoinType.Left,s.ClientId==w.ClientId,
                   JoinType.Left,s.ClientId==x.ClientId
                 })
                .Where((s, u, w, x) => x.UserId== bootstrap.UserId && s.ClientId == u.ClientId && s.DeviceId == w.DeviceId 
                && s.ClientId == w.ClientId && s.ClientId == x.ClientId)
                .Select((s, u, w, x) => new
                {
                    ClientId = s.ClientId.ToString(),
                    u.ClientName,
                    DeviceId = w.DeviceId.ToString(),
                    HistoryId = s.HistoryId.ToString(),
                    w.DeviceName,
                    s.Inspector,
                    s.InspectionTime,
                    s.CreateTime,
                    s.IsNormal,
                    s.Remark
                }).MergeTable();



            if (!bootstrap.searchpara1.IsEmpty())
            {
                query.Where((s) => s.ClientName.Contains(bootstrap.searchpara1));
            }
            if (!bootstrap.searchpara2.IsEmpty())
            {
                query.Where((s) => s.DeviceName.Contains(bootstrap.searchpara2));
            }
            if (!bootstrap.searchpara3.IsEmpty())
            {
                query.Where((s) => s.Inspector.Contains(bootstrap.searchpara3));
            }

            if (!bootstrap.datemin.IsEmpty() && !bootstrap.datemax.IsEmpty())
            {
                query.Where(s => s.CreateTime > bootstrap.datemin.ToDateTimeB() && s.CreateTime <= bootstrap.datemax.ToDateTimeE());
            }

            if (bootstrap.order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} desc");
            }
            else
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} asc");
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }
    }
}